package br.com.banco.control;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.banco.Logon;
import br.com.banco.entity.Usuario;

/**
 * Servlet implementation class Login
 */
public class Login extends HttpServlet {

    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Login() {
        super();
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        final Usuario usuario = new Logon().login(request.getParameter("nome"),
                request.getParameter("senha"));

        if (usuario == null) {
            request.setAttribute("erros", "Acesso Negado!");
            request.getRequestDispatcher("acessoRestrito.jsp").forward(request, response);
        } else {
            request.setAttribute("usuario", usuario);

            request.getRequestDispatcher("menuOpcoes.jsp").forward(request,
                    response);
        }

    }
}
